﻿<%@ Page Language="C#" AutoEventWireup="true" Inherits="System.Web.UI.Page" %>
<%@ Register Assembly="IZ.WebFileManager" Namespace="IZ.WebFileManager" TagPrefix="iz" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<%@ Import Namespace="IZ.WebFileManager" %>

<html xmlns="http://www.w3.org/1999/xhtml" >
<head runat="server">
    <title></title>
    <script type="text/javascript">
        function itemClicked(path) {
            parent.nicEdit_InsertFile(path, prompt('Enter the target location for the file (_blank is new window)','_blank'))
        }
    </script>
    <script runat="server">
        protected override void OnLoad(EventArgs e)
        {
            base.OnLoad(e);

            this.izFileManager.Visible = OpenQuarters.WebQuarters.Core.User.Current.HasPermission(OpenQuarters.WebQuarters.Core.Permissions.CMSPage.Edit.SetValue(OpenQuarters.WebQuarters.Core.CMSPage.Current.PageId.ToString()));
            this.lblNotAuthorised.Visible = !OpenQuarters.WebQuarters.Core.User.Current.HasPermission(OpenQuarters.WebQuarters.Core.Permissions.CMSPage.Edit.SetValue(OpenQuarters.WebQuarters.Core.CMSPage.Current.PageId.ToString()));
        }
        
        protected void izFileManager_ExecuteCommand(object sender, ExecuteCommandEventArgs e)
        {
            switch (e.CommandName)
            {
                case "InsertImage":
                    string path = e.Items[0].AbsolutePath.Substring(this.ResolveUrl(izFileManager.RootDirectories[0].DirectoryPath).Length);
                    OpenQuarters.WebQuarters.Utils.Image.ImageDimensions dimensions = OpenQuarters.WebQuarters.Utils.Image.GetImageSize(Server.MapPath(e.Items[0].AbsolutePath));
                    string script = @"
                            var alt = '" + System.IO.Path.GetFileName(e.Items[0].AbsolutePath) + @"';                            
                            var width = '" + dimensions.width + @"px';
                            width = prompt('Please enter the desired image width', width);
                            var height = '" + dimensions.height + @"px';
                            height = prompt('Please enter the desired image height', height);
                            width = width.replace(' ', '').replace('px', '');
                            width = width + 'px';
                            height = height.replace(' ', '').replace('px', '');
                            height = height + 'px';                            
                            alt = prompt('Please enter the image title/alt text', alt);                                                       
                            parent.nicEdit_InsertImage('" + e.Items[0].AbsolutePath + "', alt, width, height);";
                    e.ClientScript = script;
                    
                    break;
                default:
                    throw new InvalidOperationException();
            }
            //e.ClientScript = "alert('Use ExecuteCommand event to handle your custom command.\\nCommandName=" + e.CommandName + "\\nItem=" + e.Items[0].AbsolutePath + "')";            e.ClientScript = "alert('Use ExecuteCommand event to handle your custom command.\\nCommandName=" + e.CommandName + "\\nItem=" + e.Items[0].AbsolutePath + "')";
        }
        
        protected void izFileManager_ToolbarCommand(object sender, CommandEventArgs e)
        {
            switch (e.CommandName)
            {
                case "CreateZip":

                    string zipFile = System.IO.Path.Combine(izFileManager.CurrentDirectory.PhysicalPath, "ZIP_" + DateTime.Now.ToString("yyyyMMddHHmmss") + ".zip");

                    //Create an empty zip file
                    byte[] emptyzip = new byte[] { 80, 75, 5, 6, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 };

                    using (System.IO.FileStream fs = System.IO.File.Create(zipFile))
                    {
                        fs.Write(emptyzip, 0, emptyzip.Length);
                        fs.Flush();
                        fs.Close();
                    }

                    string[] selectedItems = new string[izFileManager.SelectedItems.Length];
                    for (int i = 0; i < izFileManager.SelectedItems.Length; i++)
                    {
                        selectedItems[i] = System.IO.Path.GetFileName(izFileManager.SelectedItems[i].PhysicalPath);
                    }
                    //Copy a folder and its contents into the newly created zip file
                    Shell32.ShellClass sc = new Shell32.ShellClass();
                    Shell32.Folder DestFlder = sc.NameSpace(zipFile);
                    Shell32.Folder SrcFlder = sc.NameSpace(izFileManager.CurrentDirectory.PhysicalPath);
                    Shell32.FolderItems items = SrcFlder.Items();
                    foreach (Shell32.FolderItem item in items)
                    {
                        if (Array.LastIndexOf<string>(selectedItems, item.Name) >= 0)
                            DestFlder.CopyHere(item, 20);
                    }

                    //Ziping a file using the Windows Shell API creates another thread where the zipping is executed.
                    //This means that it is possible that this console app would end before the zipping thread 
                    //starts to execute which would cause the zip to never occur and you will end up with just
                    //an empty zip file. So wait a second and give the zipping thread time to get started
                    System.Threading.Thread.Sleep(1000);
                    break;
                default:
                    throw new InvalidOperationException();
            }

        }
    </script>
</head>
<body>
    <form id="form1" runat="server">
    <div>
        <asp:Label ID="lblNotAuthorised" runat="server">You must be logged in to use the File Manager</asp:Label>
		<iz:FileManager ID="izFileManager" runat="server" Height="400px" Width="600" OnExecuteCommand="izFileManager_ExecuteCommand"
			ClientOpenItemFunction="itemClicked" OnToolbarCommand="izFileManager_ToolbarCommand" ShowHiddenFilesAndFolders="false"
		 	ShowAddressBar="false">
			<RootDirectories>
				<iz:RootDirectory DirectoryPath="~/Areas/CMS/Content/UserFiles" Text="User Files" />
			</RootDirectories>
			<CustomToolbarButtons>
				<iz:CustomToolbarButton Text="Create Zip" CommandName="CreateZip" ImageUrl="/CMS/File/Resource?Assembly=OpenQuarters.WebQuarters.Core.Web&Filename=Areas.CMS.Content.Images.IZWebFileManager.small.zip.gif" />
			</CustomToolbarButtons>
			<ToolbarOptions ShowRefreshButton="true" />
			<FileTypes>
				<iz:FileType Extensions="zip, rar, iso, gz, 7z" Name="Archive" SmallImageUrl="/CMS/File/Resource?Assembly=OpenQuarters.WebQuarters.Core.Web&Filename=Areas.CMS.Content.Images.IZWebFileManager.small.compressed.gif"
					LargeImageUrl="/CMS/File/Resource?Assembly=OpenQuarters.WebQuarters.Core.Web&Filename=Areas.CMS.Content.Images.IZWebFileManager.large.compressed.gif">
				</iz:FileType>
				<iz:FileType Extensions="doc, rtf, docx" Name="Microsoft Word Document" SmallImageUrl="/CMS/File/Resource?Assembly=OpenQuarters.WebQuarters.Core.Web&Filename=Areas.CMS.Content.Images.IZWebFileManager.small.word.gif"
					LargeImageUrl="/CMS/File/Resource?Assembly=OpenQuarters.WebQuarters.Core.Web&Filename=Areas.CMS.Content.Images.IZWebFileManager.large.word.gif">
				</iz:FileType>
				<iz:FileType Extensions="xls, csv, xlsx" Name="Microsoft Excel Worksheet" SmallImageUrl="/CMS/File/Resource?Assembly=OpenQuarters.WebQuarters.Core.Web&Filename=Areas.CMS.Content.Images.IZWebFileManager.small.excel.gif"
					LargeImageUrl="/CMS/File/Resource?Assembly=OpenQuarters.WebQuarters.Core.Web&Filename=Areas.CMS.Content.Images.IZWebFileManager.large.excel.gif">
				</iz:FileType>
				<iz:FileType Extensions="ppt, pps, pptx" Name="Microsoft PowerPoint Presentation" SmallImageUrl="/CMS/File/Resource?Assembly=OpenQuarters.WebQuarters.Core.Web&Filename=Areas.CMS.Content.Images.IZWebFileManager.small.PowerPoint.gif"
					LargeImageUrl="/CMS/File/Resource?Assembly=OpenQuarters.WebQuarters.Core.Web&Filename=Areas.CMS.Content.Images.IZWebFileManager.large.PowerPoint.gif">
				</iz:FileType>
				<iz:FileType Extensions="gif, jpg, png, bmp, jpeg" Name="Image" SmallImageUrl="/CMS/File/Resource?Assembly=OpenQuarters.WebQuarters.Core.Web&Filename=Areas.CMS.Content.Images.IZWebFileManager.small.image.gif"
					LargeImageUrl="/CMS/File/Resource?Assembly=OpenQuarters.WebQuarters.Core.Web&Filename=Areas.CMS.Content.Images.IZWebFileManager.large.image.gif">
					<Commands>
					    <iz:FileManagerCommand Name="Insert Image" CommandName="InsertImage" SmallImageUrl="/CMS/File/Resource?Assembly=OpenQuarters.WebQuarters.Core.Web&Filename=Areas.CMS.Content.Images.IZWebFileManager.small.image.gif" />
					</Commands>
				</iz:FileType>
				<iz:FileType SmallImageUrl="/CMS/File/Resource?Assembly=OpenQuarters.WebQuarters.Core.Web&Filename=Areas.CMS.Content.Images.IZWebFileManager.small.media.gif" Name="Windows Media File" Extensions="mp3,wma,vmv,avi,divx,mpeg,mpg,mp4,mkv,mov"
					LargeImageUrl="/CMS/File/Resource?Assembly=OpenQuarters.WebQuarters.Core.Web&Filename=Areas.CMS.Content.Images.IZWebFileManager.large.media.gif">
				</iz:FileType>
				<iz:FileType Extensions="txt" Name="Text Document">
				</iz:FileType>
				<iz:FileType Extensions="xml, xsl, xsd" Name="XML Document" LargeImageUrl="/CMS/File/Resource?Assembly=OpenQuarters.WebQuarters.Core.Web&Filename=Areas.CMS.Content.Images.IZWebFileManager.large.xml.gif"
					SmallImageUrl="/CMS/File/Resource?Assembly=OpenQuarters.WebQuarters.Core.Web&Filename=Areas.CMS.Content.Images.IZWebFileManager.small.xml.gif">
				</iz:FileType>
				<iz:FileType Extensions="css" Name="Cascading Style Sheet" LargeImageUrl="/CMS/File/Resource?Assembly=OpenQuarters.WebQuarters.Core.Web&Filename=Areas.CMS.Content.Images.IZWebFileManager.large.styleSheet.gif"
					SmallImageUrl="/CMS/File/Resource?Assembly=OpenQuarters.WebQuarters.Core.Web&Filename=Areas.CMS.Content.Images.IZWebFileManager.small.styleSheet.gif">
				</iz:FileType>
				<iz:FileType Extensions="js, vbs" Name="Script File" LargeImageUrl="/CMS/File/Resource?Assembly=OpenQuarters.WebQuarters.Core.Web&Filename=Areas.CMS.Content.Images.IZWebFileManager.large.script.gif"
					SmallImageUrl="/CMS/File/Resource?Assembly=OpenQuarters.WebQuarters.Core.Web&Filename=Areas.CMS.Content.Images.IZWebFileManager.small.script.gif">
				</iz:FileType>
				<iz:FileType Extensions="htm, html" Name="HTML Document" LargeImageUrl="/CMS/File/Resource?Assembly=OpenQuarters.WebQuarters.Core.Web&Filename=Areas.CMS.Content.Images.IZWebFileManager.large.html.gif"
					SmallImageUrl="/CMS/File/Resource?Assembly=OpenQuarters.WebQuarters.Core.Web&Filename=Areas.CMS.Content.Images.IZWebFileManager.small.html.gif">
				</iz:FileType>
			</FileTypes>
		</iz:FileManager>
    </div>
    </form>
</body>
</html>
